package com.scoy.honeymei.activity.mall;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.GoodsBean;
import com.scoy.honeymei.bean.GoodsOrderBean;
import com.scoy.honeymei.custem.CartEvent;
import com.scoy.honeymei.dialog.ShareDialog;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.install.GlideImageLoader;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import org.greenrobot.eventbus.EventBus;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: GoodsDetailActivity$
 * @Description: 商品详情
 * @Author: GaoZhan
 * @CreateDate: 2019/9/3/0003$ 上午 10:08$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/3/0003$ 上午 10:08$
 * @UpdateRemark: 更新内容
 */
public class GoodsDetailActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.gd_banner)
    Banner gdBanner;
    @BindView(R.id.gd_name_tv)
    TextView gdNameTv;
    @BindView(R.id.gd_shoop_tv)
    TextView gdShoopTv;
    @BindView(R.id.gd_price_tv)
    TextView gdPriceTv;
    @BindView(R.id.ct_subtract_tv)
    TextView ctSubtractTv;
    @BindView(R.id.ct_num_tv)
    TextView ctNumTv;
    @BindView(R.id.ct_add_tv)
    TextView ctAddTv;
    @BindView(R.id.gd_web)
    WebView gdWeb;
    @BindView(R.id.gd_out_tv)
    TextView gdOutTv;
    @BindView(R.id.gd_cart_tv)
    TextView gdCartTv;
    @BindView(R.id.gd_addtocart_tv)
    TextView gdAddtocartTv;
    @BindView(R.id.gd_paynow_tv)
    TextView gdPaynowTv;
    @BindView(R.id.gd_no_tv)
    TextView gdNoTv;
    private GoodsDetailActivity mContext;
    private String goodsId;
    private GoodsOrderBean goodsBean;
    private String fxUrl;
    private String fxName;
    private String fxImage;
    private String fxContent;
    private int shopId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activiy_detail_goods);
        ButterKnife.bind(this);
        mContext = GoodsDetailActivity.this;
        goodsId = getIntent().getStringExtra("goodsId");

        initNormal();

        httpGoodsDetail();

    }

    @Override
    public void initNormal() {
        titleTv.setText(R.string.goodsdetail);

        MyUtil.setStatusBar(mContext, getWindow(), true, 0);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        initWeb(gdWeb, MyUrl.PREFIX_PIC + "/api/goodsdetail/index?id=" + goodsId);

        signTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_share_gray, 0, 0, 0);
        signTv.setText("分享");

        Uri uri = getIntent().getData();
        if (uri != null) {
            String tttid = uri.getQueryParameter("id");
            goodsId = tttid;
        }
    }

    @OnClick({R.id.back_iv, R.id.ct_subtract_tv, R.id.ct_add_tv, R.id.gd_cart_tv,
            R.id.gd_addtocart_tv, R.id.gd_paynow_tv, R.id.sign_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.sign_tv:
                ShareDialog.newInstance(fxImage, fxName, fxContent, fxUrl).show(getSupportFragmentManager(), "");
                break;
            case R.id.ct_subtract_tv://减
                int iSub = Integer.parseInt(ctNumTv.getText().toString());
                if (iSub > 1) {
                    ctNumTv.setText((iSub - 1) + "");
                    goodsBean.setNumber(iSub - 1);
                } else {
                    MyUtil.mytoast0(mContext, "不能再少了");
                }
                break;
            case R.id.ct_add_tv://加
                int iAdd = Integer.parseInt(ctNumTv.getText().toString());
                ctNumTv.setText((iAdd + 1) + "");
                goodsBean.setNumber(iAdd + 1);
                break;
            case R.id.gd_cart_tv:
                setResult(0x871);
                finish();
                break;
            case R.id.gd_addtocart_tv:
                if (MyUtil.isFastClick()) {
                    httpAddCart();
                }
                break;
            case R.id.gd_paynow_tv:
                Intent intent = new Intent(mContext, PaySureActivity.class);
                intent.putExtra("goodsBean", goodsBean);
                intent.putExtra("type", 1);
                intent.putExtra("shopId", shopId);
                startActivityForResult(intent, 0x871);
                break;
        }
    }

    //商品详情http
    private void httpGoodsDetail() {
        HttpParams params = new HttpParams();
        params.put("id", goodsId);
        HpGo.newInstance().HttpGo(mContext, MyUrl.GOODS_DETAIL, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                goodsBean = new Gson().fromJson(o, GoodsOrderBean.class);

                gdNameTv.setText(goodsBean.getName());
                gdShoopTv.setText(goodsBean.getShop_name() + "的店铺");
                gdPriceTv.setText("￥" + goodsBean.getMoney());

                shopId = goodsBean.getShop_id();

                goodsBean.setNumber(1);

                String status = goodsBean.getStatus();//0已下架 1未下架
                if ("0".equals(status)) {
                    gdNoTv.setVisibility(View.VISIBLE);
                    gdPaynowTv.setEnabled(false);
                    gdAddtocartTv.setEnabled(false);
                }

                String images = goodsBean.getImages();
                if (images != null) {
                    String[] split = images.split(",");
                    List<String> imagelist = Arrays.asList(split);
                    initBanner(gdBanner, imagelist);
                }

                fxUrl = goodsBean.getFx_url();
                fxName = goodsBean.getFx_title();
                fxImage = MyUrl.PREFIX_PIC + goodsBean.getFx_image();
                fxContent = goodsBean.getFx_miaoshu();

            }
        });

    }

    //加入购物车http
    private void httpAddCart() {
        HttpParams params = new HttpParams();
        params.put("id", goodsId);
        params.put("userid", SPHelper.getString(MeConstant.UID, ""));
        params.put("number", ctNumTv.getText().toString());
        HpGo.newInstance().HttpGo(mContext, MyUrl.ADD_TOCART, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                EventBus.getDefault().post(new CartEvent());
                MyUtil.mytoast0(mContext, "添加成功", R.mipmap.ic_sure);
            }
        });

    }


    /**
     * banner图
     */
    public void initBanner(Banner banner, List<String> bannerimglist) {
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(bannerimglist);
        banner.setBannerAnimation(Transformer.Default);
        banner.isAutoPlay(true);
        banner.setDelayTime(4000);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {

               /* if (MyUtil.isFastClick() && CheckUtil.checkIfUrl("id")) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("id")));
                } else {
                    MyUtil.mytoast(mContext, "链接有误，无法跳转");
                }*/

            }
        });
        banner.start();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 0x87:
                finish();
                startActivity(new Intent(mContext, MallMeActivity.class));
                break;
        }
    }
}
